#!/usr/local/bin/ruby

require 'rubygems'
require 'twitter'
require 'time'

OAUTH_CONSUMER_KEY    = ''
OAUTH_CONSUMER_SECRET = ''
OAUTH_ACCESS_TOKEN    = ''
OAUTH_ACCESS_SECRET   = ''

oauth = Twitter::OAuth.new( OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET )
oauth.authorize_from_access( OAUTH_ACCESS_TOKEN, OAUTH_ACCESS_SECRET )
client = Twitter::Base.new( oauth )


def request( client, function, options, argvs )

  case function
  when '-r'
    return client.replies( { :count => options[ :count ], :page => options[ :page ] } )
  when '-me'
    return client.user_timeline( { :count => options[ :count ], :page => options[ :page ] } )
  when '-u'
    return client.user_timeline( { :id => argvs[ 0 ], :count => options[ :count ], :page => options[ :page ] } )
  else
    return client.friends_timeline( { :count => options[ :count ], :page => options[ :page ] } )
  end

end


def get_option_value( symbol )
  result = nil
  if ARGV.index( symbol ) && ( ! ARGV.index( '-p' ) || ARGV.index( '-p' ) > ARGV.index( symbol ) ) then
    result = ARGV[ ARGV.index( symbol ) + 1 ]
    ARGV.slice!( ARGV.index( symbol ), 2  )
  end
  return result
end


option_values = { }
option_values[ :page ]  = get_option_value( '-o' ) 
option_values[ :count ] = get_option_value( '-l' )


option = ARGV.size > 0 && ARGV[ 0 ].slice( 0..0 ) == '-' ? ARGV.shift : nil

if option == '-p' && ARGV.size > 0 then
  client.update( ARGV.join( ' ' ) )
end

request( client, option, option_values, ARGV ).reverse.each do |s|
  time = Time.parse( s.created_at )
  puts "#{s.user.screen_name} : #{s.text} [ #{Time.parse( s.created_at ).strftime( '%Y/%m/%d %H:%M' )} ]"
end

